package com.tingyuge.thinking.spring.resource;

import com.tingyuge.thinking.spring.resource.util.ResourceUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;

import javax.annotation.PostConstruct;
import java.util.stream.Stream;

/**
 * 注入 {@link org.springframework.core.io.Resource} 对象示例
 *
 * @author xiaodao
 * @version 1.0
 * @date 2021/8/22
 * @see org.springframework.beans.factory.annotation.Value
 * @see org.springframework.context.annotation.AnnotationConfigApplicationContext
 */
public class InjectResourceDemo {

    @Value("classpath:/META-INF/default.properties")
    private Resource defaultProperties;

    @Value("classpath*:/META-INF/*.properties")
    private Resource[] propertiesResources;

    @Value("${user.dir}")
    private String currentProjectRootPath;

    @PostConstruct
    public void init(){
        System.out.println(ResourceUtils.getContent(defaultProperties));
        System.out.println(currentProjectRootPath);
        System.out.println("--------");
        Stream.of(propertiesResources).map(ResourceUtils::getContent).forEach(System.out::println);
    }
    public static void main(String[] args) {
        //创建beanFactory容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(InjectResourceDemo.class);
        //启动应用上下文
        applicationContext.refresh();
    }


}
